<?php

namespace App\Http\Controllers\Admin;

use App\Http\Util\Err;
use App\Http\Util\Upload;
use App\Http\Controllers\Controller;
use App\Models\Device;
use App\Models\TestRecord;
use App\Models\User;
use App\Tools\Helper;
use App\Utils\Result;
use App\Utils\Token;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use RebaseData\Converter\Converter;
use RebaseData\InputFile\InputFile;
class TestController extends Controller
{
    /**产测接口
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function testRecord(Request $request): \Illuminate\Http\JsonResponse
    {
        $validData = $request->validate([
            'mac' => 'required',
            'sn' => 'required',
            'name' => 'required',
            'overload_i' => 'required',
            'step_2' => 'required',
            'step_3' => 'required',
            'step_4' => 'required',
            'error_ratio' => 'required',
            'device_type' => 'required|in:' . Helper::keysToString(TestRecord::DEVICE_TYPE_MAP),
        ], [], [
            'mac' => 'mac地址',
            'sn' => '二维码编号',
            'name' => '蓝牙名称',
            'overload_i' => '过载电流',
            'step_2' => '校表',
            'step_3' => '开闸/合闸',
            'step_4' => '清表',
            'error_ratio' => '精度误差',
            'device_type' => '设备类型',
        ]);

        if(Str::contains($request->sn, 'sn=')) {
            $validData['sn'] = explode('sn=', $request->sn)[1];
        }

        if(TestRecord::whereSn($request->sn)->exists()) {
            return Result::error(400,'该二维码设备已经测试过了');
        }

        if(TestRecord::where('name', $request->name)->exists()) {
            return Result::error(400,'该蓝牙设备已经测试过了');
        }
        $is_api = $request->input('is_api',1);
        $distributor_mobile = $request->input('distributor_mobile','');
        if(!$distributor_mobile){
            return Result::error(400,'请填写经销商手机号');
        }
        $mac = str_replace(":", "", $request->input('mac'));
        $sn = $request->input('sn');
        $deviceModel = Device::query()->where('mac', $mac)
            ->orWhere('sn', $sn)->first();
        if ($deviceModel && $deviceModel->mac == $mac) {
            return Result::error(401, 'MAC地址已存在');
        }
        if ($deviceModel) {
            return Result::error(401, '二维码编号已存在');
        }

        $user = User::query()->where('email',$distributor_mobile)->first();

        if($user){
            $userId = $user->id;
        }else{
            $userId = 0;
        }

        $testRecord = new TestRecord($validData);
        $testRecord->user_id = $userId;
        $testRecord->mobile = $distributor_mobile;
        $testRecord->save();
        $type = $request->input('type',1);
        $deviceModel = new Device();
        $deviceModel->is_api = $is_api;
        $deviceModel->mac = $testRecord->mac;
        $deviceModel->name = $testRecord->name;
        $deviceModel->sn = $testRecord->sn;
        $deviceModel->user_id = $userId;
        $deviceModel->type = $type;
        $deviceModel->pay_mode = $request->input('pay_mode') == 2 ? 2 : 1;
        $deviceModel->net_type = $request->input('device_type') == 1 ? 1 : 2;
        if ($deviceModel->net_type == 1) {
            $deviceModel->is_master = 1;
        }
        $deviceModel->overload = $testRecord->overload_i;
        $deviceModel->expired_at = Carbon::now()->addYears(8)->toDateTimeString();
        $deviceModel->save();
        return Result::success();
    }

}
